<?php
/*
 * 手机客户端帐户-我的店铺-订单列表接口
 * 2014-03-11
 * 2014-05-08 (已查)
 */
include "inc/public_header.php";

include "func/user.php";				//用户信息处理函数


$reJson = array(); //构建返回值

$IMG_BASE_URL = "http://www.chinalaohu.com/modules/miniature.php?w=80&h=80&p=/uploads/shop/productImg/";

$type 		= $_REQUEST["type"];
$page			= $_REQUEST["page"]; 			//页数，第一页为0
$pagesize		= $_REQUEST["pagesize"];		//每页记录数

if(!isset($page)){
	$page = 0;
}
if(!isset($pagesize)){
	$pagesize = 5; //默认每页15条
}

//获取用户信息
$uInfoArr = user_getInfoFromUUID();
if(!$uInfoArr){
	echo json_encode(array("success"=>"0","errCode"=>"0","msg"=>"没有获取到用户信息"));
	die;
}
$uid = $uInfoArr["uid"];//用户信息主键
//$uid = "10739";
$business_type = 0;
if ($uid){
$sql = "SELECT business_type FROM user_base where uid='".$uid."'";
$db->query($sql);
if ($db->next_record()){
	$business_type = $db->f("business_type");
}
}
       
$reJson["success"] = "1";

//订单条数
$sql = "select count(t1.id) as ct from `shop_order` as t1,`shop_order_history` as t2,`shop_info` as t3   where t1.id=t2.order_id  and   t1.uid='".$uid."' and t1.del_type='0' and t1.shop_id=t3.shop_id";
if ($type){
$sql = $sql." and t2.order_status_id='".$type."' ";
}
$db1->query($sql);
$db1->next_record();
$reJson["count"] = $db1->f("ct");

//商品订单列表
$sql = "select t1.id,t1.tprice,t3.shop_id  from `shop_order` as t1,`shop_order_history` as t2,`shop_info` as t3   where t1.id=t2.order_id  and   t1.uid='".$uid."' and t1.del_type='0' and t1.shop_id=t3.shop_id"; 
$sql = $sql." and t2.order_status_id='1' ";
$sql = $sql." order by t1.id desc ";
$sql = $sql." limit ".($page * $pagesize).",".$pagesize;
$db->query($sql);
while($db->next_record()){
	$id	   		= $db->f("id");
	$ttprice	   	= $db->f("tprice");
	$shop_id	   	= $db->f("shop_id");
	if ($shop_id!='81' and $shop_id!='86' and $shop_id!='87' and $shop_id!='88' and $shop_id!='92' and $shop_id!='93'){
	$db2->query("select  t2.`price`,t2.`qyprice`,t1.`product_id`,t1.`tprice`,t1.`tcount` from `shop_order_detail` as t1,`shop_product` as t2 where t1.product_id=t2.product_id  and  t1.pid='".$id."'");
	while($db2->next_record()){
		if ($business_type==1 and $db2->f("qyprice") and $db2->f("qyprice")<$db2->f("price")){
		$price	   	  = $db2->f("qyprice");
		}else{
		$price	   	  = $db2->f("price");
		}
		$product_id	  = $db2->f("product_id");
		$tprice	   	  = $db2->f("tprice");
		$tcount	   	  = $db2->f("tcount");
		if ($price*$tcount!=$tprice){
			$sql3 = "update shop_order_detail set tprice='".$price*$tcount."' where product_id='".$product_id."'";
			$db3->query($sql3);
			$arr1[] = $price*$tcount;
		}else{
			$arr1[] = $tprice;
		}
	}
	if ($ttprice!=array_sum($arr1)){
		$sql2 = "update shop_order set tprice='".array_sum($arr1)."' where id='".$id."'";
		$db2->query($sql2);
	}
	$arr1 = "";
	}
}			

$sql = "select t1.id,t1.tprice,t1.tcount,t1.yunfei,t3.shop_name,t2.order_status_id,t1.wuliuname,t1.wuliuno  from `shop_order` as t1,`shop_order_history` as t2,`shop_info` as t3   where t1.id=t2.order_id  and   t1.uid='".$uid."' and t1.del_type='0' and t1.shop_id=t3.shop_id"; 
if ($type){
	if ($type==6){
	$sql = $sql." and t2.order_status_id in ('2','3')";
	}else{
	$sql = $sql." and t2.order_status_id='".$type."' ";
	}
}
$sql = $sql." order by t1.id desc ";
$sql = $sql." limit ".($page * $pagesize).",".$pagesize;
$db->query($sql);
$orderArr = array();
$goodsArr = array();
while($db->next_record()){
	$id	   		= $db->f("id");
	$shop_name	= $db->f("shop_name");
	$order_status_id = $db->f("order_status_id");
	$wuliuname = $db->f("wuliuname");
	$wuliuno = $db->f("wuliuno");
	$tprice	   	= $db->f("tprice");
	$tcount	   	= $db->f("tcount");
	$yunfei	   	= $db->f("yunfei");
	$totalPrice = $tprice+$yunfei;
	$sql="select 
				(select count(*) from shop_order a
							INNER JOIN
							shop_order_detail b on a.id=b.pid
							INNER JOIN
							shop_order_history c on a.id=c.order_id
							where a.id='".$id."' and a.uid='".$uid."' and c.order_status_id='5') as c1,
				(SELECT count(*) from shop_product_assess where uid='".$uid."' and order_id='".$id."') as c2";
	$db1->query($sql);
	if($db1->next_record()){
		$c1 = $db1->f('c1');
		$c2 = $db1->f('c2');
	}
	if ($c1==$c2){
	$comment="0";
	}else{
	$comment="1";
	}
	if ($order_status_id==3 or $order_status_id==5){
	$logistics="http://adio.chinalaohu.com/app/doc/wuliu.php?wuliuname=".$wuliuname."&wuliuno=".$wuliuno."";
	}else{
	$logistics="";
	}
	$db2->query("select  t2.`price`,t2.`qyprice`,t2.`product_id`,t2.`product_name`,t2.image,t1.`tcount` from `shop_order_detail` as t1,`shop_product` as t2 where t1.product_id=t2.product_id  and  t1.pid='".$id."'");
	while($db2->next_record()){
		$product_id = $db2->f("product_id");
		$product_name = $db2->f("product_name");
		$image		  = $db2->f("image");
		if ($business_type==1 and $db2->f("qyprice") and $db2->f("qyprice")<$db2->f("price")){
		$price	   	  = $db2->f("qyprice");
		}else{
		$price	   	  = $db2->f("price");
		}
		$ttcount	   	  = $db2->f("tcount");
		$goodsArr[] = array(
			"imgUrl"=>$IMG_BASE_URL.$image					
			,"goodsId"=>$product_id	
			,"goodsName"=>$product_name	
			,"goodsCount"=>$ttcount
			,"goodsPrice"=>$price	
		);
	}
	$orderArr[] = array(
		"orderId"=>$id					
		,"shopName"=>$shop_name
		,"type"=>$order_status_id
		,"totalPrice"=>"$totalPrice"
		,"totalCount"=>$tcount
		,"comment"=>$comment
		,"logistics"=>$logistics
		,"goods"=>$goodsArr		
	);
	$goodsArr = "";
}			
//放入商品评价列表
$reJson["list"] = $orderArr;

echo json_encode($reJson);
?>
